package org.springframework.core.io;

import java.io.IOException;
import java.io.InputStream;

import org.springframework.util.ClassUtils;

public class ClassPathResource implements Resource {

    private String path;
    
    private Class clazz;
    
    private ClassLoader classLoader;
    
    public ClassPathResource(String path, Class clazz) {
        this.path = path;
        this.clazz = clazz;
    }

    public ClassPathResource(String path) {
        this(path, (ClassLoader) null);
    }
    public ClassPathResource(String path, ClassLoader classLoader) {
        this.path = path;
        this.classLoader = classLoader != null ?  classLoader : ClassUtils.getDefaultClassLoader();
    }

    @Override
    public InputStream getInputStream() throws IOException {
        InputStream is = null;
        if (this.clazz != null) {
            is = this.clazz.getResourceAsStream(this.path);
        } else {
            is = this.classLoader.getResourceAsStream(this.path);
        }
        return is;
    }
}
